<?php

namespace App\Console\Commands;

use App\Models\TgChannel;
use App\Models\User;
use App\Services\BlockChainServices;
use Illuminate\Console\Command;

class AddressSetRebuild extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
        protected $signature = 'address:rebuild';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {

        $start = time();
        $this->line("start");

        $chain = ["TRON"];
        foreach ($chain as $v) {
            BlockChainServices::contractFlush($v);
            BlockChainServices::addressFlush($v);
        }

        $contract = config("coins.contract");
        foreach ($contract as $k => $v) {
            if ($k == "TRON") {
                BlockChainServices::contractSet($k, strtolower($v));
            }
        }

        $contract = config("coins.withdraw_contract");
        foreach ($contract as $k => $v) {
            if ($k == "TRON") {
                BlockChainServices::contractSet($k, strtolower($v));
            }
        }
        $users = TgChannel::query()->select("channel_address")->pluck('channel_address')->toArray();
        $users_new = [];
        foreach ($users as $v) {
            if ($v) {
                $users_new[] = ($v);
            }
        }

        if (count($users_new) > 0) {
            BlockChainServices::addressSet("TRON", $users_new);
        }
        $end = time() - $start;
        $this->line("end ($end)");
        return 1;
    }
}
